package com.mirstone.jetpackdemo.room;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.mirstone.jetpackdemo.App;
import com.mirstone.jetpackdemo.R;
import com.mirstone.jetpackdemo.databinding.ActivityRoomBinding;

import java.util.List;

public class RoomActivity extends AppCompatActivity {

    private ActivityRoomBinding binding;
    private RecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_room);


        binding.setOnAddClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(RoomActivity.this, AddUserActivity.class));
            }
        });

        binding.setOnDeleteAllClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.getAppDataBase().userDao().deleteAll();
            }
        });

        adapter = new RecyclerAdapter();
        binding.recycler.setAdapter(adapter);
        App.getAppDataBase().userDao().getAll().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                adapter.update(users);
            }
        });
    }
}